package com.yztc.ui.classifydetail;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.yztc.bean.classifydetail.ClassifyDetailHeaderActlistBean;
import com.yztc.bean.classifydetail.ClassifyDetailHeaderBean;
import com.yztc.bean.classifydetail.ClassifyDetailHeaderSecondlevelBean;
import com.yztc.bean.classifydetail.ClassifyProductListBean;
import com.yztc.chunboapp.R;
import com.yztc.ui.WebViewActivity;
import com.yztc.ui.classifydetail.myadapter.ClassifyDetailHeaderActlistAdapter;
import com.yztc.ui.classifydetail.myadapter.ClassifyDetailProductAdapter;
import com.yztc.ui.details.DetailsActivity;
import com.yztc.utils.ImageHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yztc.chunboapp.R.layout.item_classify_detail_header;

public class ClassifyDetailActivity extends AppCompatActivity implements ClassifyDetailContract.View{
    //presenter对象
    private ClassifyDetailPresenter presenter;

    //布局控件
    private EditText et_classify_detail;
    private ImageButton ib_back_classify_detail;
    private XRecyclerView rv_classify_detail_product;
    private View viewHeader;

    private ImageView iv_classify_detail_header;
    private RecyclerView rv_classify_detail_header;
    private CheckBox ck01_classify_detail_header;
    private CheckBox ck02_classify_detail_header;

    //适配器
    private ClassifyDetailProductAdapter adapter;
    private ClassifyDetailHeaderActlistAdapter actlistBeanAdapter;

    //类、集合
    private int id;
    private Map<String,String> map=new HashMap<>();
    private List<ClassifyProductListBean> list;
    private ClassifyDetailHeaderBean classifyDetailHeaderBean;
    private List<ClassifyDetailHeaderSecondlevelBean> listHeaderSecondlevel;
    private List<ClassifyDetailHeaderActlistBean> listHeaderActlist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classify_detail);
        Log.i("tag","!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

        //初始化控件
        initView();

        //获取数据
        initData();

        Log.i("tag","################################");
        presenter=new ClassifyDetailPresenter(this,map,id);
        presenter.getVerticalClassifyDetailHeaderBean(this);
        presenter.getVerticalClassifyProductListBean(this);
        Log.i("tag","################################@@@@@@@@@@");

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        setListener();
    }


    private void setListener() {

        ib_back_classify_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ck01_classify_detail_header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ck01_classify_detail_header.isChecked()){
                    ck02_classify_detail_header.setChecked(false);
                }
            }
        });
        ck02_classify_detail_header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ck02_classify_detail_header.isChecked()){
                    ck01_classify_detail_header.setChecked(false);
                }
            }
        });

        ck01_classify_detail_header.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    ck01_classify_detail_header.setBackgroundDrawable(getResources().getDrawable(R.drawable.classify_detail_radio_true_shapes));

                    map.put("type",2+"");
                    presenter=new ClassifyDetailPresenter(ClassifyDetailActivity.this,map,id);
                    presenter.getVerticalClassifyProductListBean(ClassifyDetailActivity.this);
                    adapter.notifyDataSetChanged();
                }else{
                    ck01_classify_detail_header.setBackgroundDrawable(getResources().getDrawable(R.drawable.classify_detail_radio_false_shapes));

                    map.put("type",1+"");
                    presenter=new ClassifyDetailPresenter(ClassifyDetailActivity.this,map,id);
                    presenter.getVerticalClassifyProductListBean(ClassifyDetailActivity.this);
                    adapter.notifyDataSetChanged();
                }
            }
        });
        ck02_classify_detail_header.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    ck02_classify_detail_header.setBackgroundDrawable(getResources().getDrawable(R.drawable.classify_detail_radio_true_shapes));

                    map.put("type",3+"");
                    presenter=new ClassifyDetailPresenter(ClassifyDetailActivity.this,map,id);
                    presenter.getVerticalClassifyProductListBean(ClassifyDetailActivity.this);
                    adapter.notifyDataSetChanged();
                }else{
                    ck02_classify_detail_header.setBackgroundDrawable(getResources().getDrawable(R.drawable.classify_detail_radio_false_shapes));

                    map.put("type",1+"");
                    presenter=new ClassifyDetailPresenter(ClassifyDetailActivity.this,map,id);
                    presenter.getVerticalClassifyProductListBean(ClassifyDetailActivity.this);
                    adapter.notifyDataSetChanged();
                }
            }
        });


    }

    private void initData() {
//      http://api.chunbo.com/Search/category_list/?cid=521&cid_l=1&order=0&type=1&page=1&page_size=20
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        String category_id=bundle.getString("category_id");
        Log.i("tag","===================================="+category_id);
        id=Integer.parseInt(category_id);

        map.put("cid",category_id);
        map.put("cid_l",1+"");
        map.put("order",0+"");
        map.put("type",1+"");
        map.put("page",1+"");
        map.put("page_size",20+"");
        Log.i("tag","~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+map);
    }

    private void initView() {
        rv_classify_detail_product= (XRecyclerView) findViewById(R.id.rv_classify_detail_product);
        rv_classify_detail_product.setLayoutManager(new GridLayoutManager(this,2));
        rv_classify_detail_product.setItemAnimator(new DefaultItemAnimator());
//        rv_classify_detail_product.addItemDecoration(new DividerGridItemDecoration(this));

        et_classify_detail= (EditText) findViewById(R.id.et_classify_detail);
        ib_back_classify_detail= (ImageButton) findViewById(R.id.ib_back_classify_detail);

        viewHeader= LayoutInflater.from(this).inflate(item_classify_detail_header,null);

        iv_classify_detail_header= (ImageView) viewHeader.findViewById(R.id.iv_classify_detail_header);

        rv_classify_detail_header= (RecyclerView) viewHeader.findViewById(R.id.rv_classify_detail_header);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv_classify_detail_header.setLayoutManager(linearLayoutManager);
        rv_classify_detail_header.setItemAnimator(new DefaultItemAnimator());

        ck01_classify_detail_header= (CheckBox) viewHeader.findViewById(R.id.ck01_classify_detail_header);
        ck02_classify_detail_header= (CheckBox) viewHeader.findViewById(R.id.ck02_classify_detail_header);

        /*rg_classify_detail_header= (RadioGroup) viewHeader.findViewById(R.id.rg_classify_detail_header);
        rb01_classify_detail_header= (RadioButton) viewHeader.findViewById(R.id.rb01_classify_detail_header);
        rb02_classify_detail_header= (RadioButton) viewHeader.findViewById(R.id.rb02_classify_detail_header);*/

    }


    @Override
    public void OnGetVerticalClassifyDetailHeaderBean(ClassifyDetailHeaderBean classifyDetailHeaderBeen02) {
        classifyDetailHeaderBean=classifyDetailHeaderBeen02;
        ImageHelper.getNormalImage(this,classifyDetailHeaderBean.getImageurl(),iv_classify_detail_header);

        iv_classify_detail_header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ClassifyDetailActivity.this, WebViewActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("link",classifyDetailHeaderBean.getAnhearturl());
                bundle.putString("title","春播标准-"+classifyDetailHeaderBean.getName());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public void OnGetVerticalClassifyDetailHeaderActlistBean(List<ClassifyDetailHeaderActlistBean> classifyDetailHeaderActlistBeen) {
        listHeaderActlist=classifyDetailHeaderActlistBeen;
        Log.i("tag","+++++++++++++++listHeaderActlist+++++++++++++"+listHeaderActlist);

        actlistBeanAdapter=new ClassifyDetailHeaderActlistAdapter(this,listHeaderActlist);
        rv_classify_detail_header.setAdapter(actlistBeanAdapter);

        actlistBeanAdapter.setOnItemClickListener(new onItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(ClassifyDetailActivity.this, DetailsActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("product_id",listHeaderActlist.get(position).getLink()+"");
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

        rv_classify_detail_product.addHeaderView(viewHeader);
    }

    @Override
    public void OnGetVerticalClassifyDetailHeaderSecondlevelBean(List<ClassifyDetailHeaderSecondlevelBean> classifyDetailHeaderSecondlevelBeen) {
        listHeaderSecondlevel=classifyDetailHeaderSecondlevelBeen;
        Log.i("tag","+++++++++++++listHeaderSecondlevel+++++++++++++++"+listHeaderSecondlevel);
    }

    @Override
    public void onGetVerticalClassifyProductListBean(List<ClassifyProductListBean> classifyProductListBeen) {
        list=classifyProductListBeen;
        Log.i("tag","+++++++++++++++list+++++++++++++"+list);

        adapter=new ClassifyDetailProductAdapter(this,list);
        rv_classify_detail_product.setAdapter(adapter);


        adapter.setOnItemClickListener(new onItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(ClassifyDetailActivity.this, DetailsActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("product_id",list.get(position).getProductId()+"");
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    @Override
    public void onGetVerticalFailed(Throwable error) {
        Toast.makeText(ClassifyDetailActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
    }

    //定义接口定义RecyclerView中的item的点击事件
    public interface onItemClickListener{
        public void onItemClick(View view,int position);
        public void onItemLongClick(View view,int position);
    }
}
